﻿using System.Collections;
using Pap.Data;

namespace Pap.Web.Modules.Loan.Views
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Coolite.Ext.Web;
    using global::Utils.UIPermission;
    using Pap.Web.Modules.Loan.ViewModel;
    using Pap.Web.Utils;
    using Ext = Coolite.Ext.Web;

    /// <summary>
    /// 
    /// </summary>
    public partial class CreateCustomer : BasePage<CreateCustomerViewModel>, IPermittedPage
    {
        #region [ Viewstate Properties ]
        /// <summary>
        /// Gets or sets the current customer ID.
        /// </summary>
        /// <value>
        /// The current customer ID.
        /// </value>
        public string CurrentCustomerID
        {
            get
            {
                return (string)this.ViewState["CustomerID"];
            }
            set
            {

                this.ViewState.Add("CustomerID", value);
            }
        }

        /// <summary>
        /// Gets or sets the name of the current customer.
        /// </summary>
        /// <value>
        /// The name of the current customer.
        /// </value>
        public string CurrentCustomerName
        {
            get
            {
                return (string)this.ViewState["CustomerName"];
            }
            set
            {
                this.ViewState.Add("CustomerName", value);
            }
        }
        #endregion

        #region [ Event Handlers ]

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Handles the Command event of the gvCustomers control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void gvCustomers_Command(object sender, Ext.AjaxEventArgs args)
        {
            switch (args.ExtraParams["CommandName"])
            {
                case "Availability":
                    ValidateCustomer(args.ExtraParams["CustomerID"], args.ExtraParams["CustomerName"]);
                    break;
                case "History":
                    if (args.ExtraParams["CustomerID"] != null && args.ExtraParams["CustomerID"].ToGuid() != Guid.Empty)
                        this.RedirectFromThisPage("customerhistory", new string[] { "CustomerID=" + args.ExtraParams["CustomerID"] });
                    else
                        this.RedirectFromThisPage("customerhistory", new string[] { "CustomerName=" + args.ExtraParams["CustomerName"] });
                    break;
                default:
                    return;
            }
        }

        /// <summary>
        /// Handles the Click event of the btnSearch control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void btnSearch_Click(object sender, Ext.AjaxEventArgs args)
        {
            string SSN = txtSSID.Text;
            string CustomerName = txtName.Text;
            HybridCustomerDisplay[] results = this.ViewModel.SearchCustomers(SSN, CustomerName);

            if (results.Length == 0)
                this.btnAddLoan.Disabled = false;
            else
                this.btnAddLoan.Disabled = true;

            this.storeCustomers.DataSource = results;
            this.storeCustomers.DataBind();

            this.storeErrors.DataSource = new ArrayList();
            this.storeErrors.DataBind();
        }

        /// <summary>
        /// Handles the Clicked event of the btnAddLoan control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void btnAddLoan_Clicked(object sender, Ext.AjaxEventArgs args)
        {
            RowSelectionModel row = gvCustomers.SelectionModel.Primary as RowSelectionModel;
            if (row.SelectedRecordID.IsNullOrEmpty())
                this.RedirectFromThisPage("Createloan");
            else
            {
                this.RedirectFromThisPage("Loan", "Createloan", new string[] { string.Format("CustomerID={0}", row.SelectedRecordID) });
            }
        }

        /// <summary>
        /// Handles the Clicked event of the btnBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void btnBack_Clicked(object sender, Ext.AjaxEventArgs args)
        {
            this.RedirectToPreviousPage();
        }
        #endregion

        #region [ Private Functions ]
        /// <summary>
        /// Validates the customer.
        /// </summary>
        /// <param name="customerID">The customer ID.</param>
        /// <param name="customerName">Name of the customer.</param>
        private void ValidateCustomer(string customerID, string customerName)
        {
            List<ValidationError> errors = this.ViewModel.ValidateCustomer(customerID, customerName);
            errors.Sort();

            if (!customerID.IsNullOrEmpty())
                CurrentCustomerID = customerID;
            if (!customerName.IsNullOrEmpty())
                CurrentCustomerName = customerName;

            gvErrors.Hidden = false;
            gvErrors.Title = (string)GetLocalResourceObject("CustomerValidation") + " " + customerName;

            if (errors.Count(e => e.IsError) == 0)
                btnAddLoan.Disabled = false;
            else
                btnAddLoan.Disabled = true;

            this.storeErrors.DataSource = errors.ToArray();
            this.storeErrors.DataBind();
        }
        #endregion

        #region [ IPermittedPage Members ]

        /// <summary>
        /// Gets the name of the permission.
        /// </summary>
        /// <value>
        /// The name of the permission.
        /// </value>
        public string PermissionName
        {
            get
            {
                return Permissions.CreateCustomer.ToString();
            }
        }

        #endregion
    }
}
